package com.myfir.service.user;

import java.util.List;

import com.myfir.dao.UserDAO;
import com.myfir.entity.User;

public class UserServiceImpl implements UserService {
	private UserDAO userDao;
	private User user;
	private List<User> users;
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserDAO getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDAO userDao) {
		this.userDao = userDao;
	}

	public void register(User user) throws Exception {
		//String pwd = DegistUtil.md5(user.getPassword());
		String pwd = user.getPassword();
		user.setPassword(pwd);
		try {
			userDao.save(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public User findById(int id) {
		try {
			user = userDao.findById(id);
			return user;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}

	public void updateById(User user) {
		userDao.updateById(user);
	}

	public User findByName(String name) {
		try {
			user = userDao.findByName(name);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}

	public List<User> findAll() {
		try {
			users = userDao.findAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return users;
	}

	public List<User> searchByKeyword(String keyword) {
		try {
			users = userDao.searchByKeyword(keyword);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return users;
	}
	
	public void delete(User user) {
		try {
			userDao.delete(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}